package amu.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import amu.database.BookDAO;
import amu.database.ReviewDAO;
import amu.model.Book;
import amu.model.Customer;
import amu.model.Review;

public class RateReviewHelpfulAction implements Action {

	@Override
	public ActionResponse execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		HttpSession session = request.getSession(true);
        Customer customer = (Customer) session.getAttribute("customer");
        
        if (customer == null) {
            ActionResponse actionResponse = new ActionResponse(ActionResponseType.REDIRECT, "loginCustomer");
            actionResponse.addParameter("from", "viewBook");
            return actionResponse;
        }
        
            int id = Integer.parseInt(request.getParameter("id"));
            ReviewDAO reviewDAO = new ReviewDAO();
            Review review = reviewDAO.read(id);
            review.setNumHelpful(review.getNumHelpful() + 1);
            if (reviewDAO.update(review)) {
            	ActionResponse actionResponse = new ActionResponse(ActionResponseType.REDIRECT, "viewBook");
            	actionResponse.addParameter("isbn", review.getBook().getIsbn13());
            	return actionResponse;
            }        	
                
		return new ActionResponse(ActionResponseType.REDIRECT, "viewBook");
	}

}
